package com.jack.database_exp.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("all")
@Configuration
public class DruidConfig{
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }
    //配置Druid监控分为两步:
    //①配置管理后台的Servlet
    //    //②配置一个监控的filter
    @Bean
    public ServletRegistrationBean statViewServlet(){
        //StatViewServlet类可以帮助我们访问到后台页面,"/druid/*"表示访问路径
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String,String> map = new HashMap<>(20);
        //指定登录的用户是admin
        map.put("loginUsername","admin");
        map.put("loginPassword","123456");
        map.put("allow","");//这里默认允许任何所有访问，如果写localhost就表示只有主机可以访问
        map.put("deny","192.168.43.228");//表示拒绝访问，我的主机IPV4地址为192.168.43.228
        bean.setInitParameters(map);
        return bean;
    }

    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        Map<String,String> map = new HashMap<>(20);
        //表示不拦截的请求
        map.put("exclutions","*.js,*.css,/druid/*");
        bean.setInitParameters(map);
        //表示拦截哪些请求
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}
